/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import general.util.EntitiesValidation;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import jpa.controller.BankJpaController;
import jpa.entity.Bank;

/**
 *
 * @author isaias
 */
@ManagedBean
@ViewScoped
public class BankBean implements Serializable{

    private String filterNameBank=null;
    private Bank bank = new Bank();
    private Bank bankSelected = new Bank();

    /** Creates a new instance of BancoBean */
    public BankBean() {
    }

    public List<Bank> getListBank(){
        return new BankJpaController().findBankAjaxName(filterNameBank);
    }

    public void delete(){
        if(bankSelected.getId() != null){
            try {
               new BankJpaController().destroy(bankSelected.getId());
                 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "Banco apagado com sucesso!", null));
            } catch (Exception e) {
                e.printStackTrace();
            }

        }else{
           FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Selecione um banco para poder remover!", null));
        }
    }

    public void edit(){
        List<String> erros = EntitiesValidation.validateBank(bankSelected);

         if(erros.isEmpty()){
              try {
                  if(bankSelected.getId() != null){
                     new BankJpaController().edit(bankSelected);
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "Banco editado com sucesso!", null));
                       bankSelected = new Bank();
                  }else{
                       FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Selecione um banco para poder editar!", null));
                  }
             } catch (Exception e) {
                       FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Erro ao tentar editar banco!", null));
                 e.printStackTrace();
             }

         }else{
             for( String e : erros){
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    e, null));
           }
         }

    }

    public void save(){
        List<String> erros = EntitiesValidation.validateBank(bank);

        if(erros.isEmpty()){
           try{
            new BankJpaController().create(bank);
             FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Banco criado com sucesso!", null));
             bank = new Bank();
            }catch(Exception ex){
                ex.printStackTrace();
                   FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Erro ao criar banco!", null));
            }
        }else{
           for( String e : erros){
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    e, null));
           }
        }
    }

    public Bank getBank() {
        return bank;
    }

    public void setBank(Bank bank) {
        this.bank = bank;
    }

    public Bank getBankSelected() {
        return bankSelected;
    }

    public void setBankSelected(Bank bankSelected) {
        this.bankSelected = bankSelected;
    }

    public String getFilterNameBank() {
        return filterNameBank;
    }

    public void setFilterNameBank(String filterNameBank) {
        this.filterNameBank = filterNameBank;
    }
     
}
